//
//  JYChatCell.m
//  Pods
//
//  Created by JY on 2017/5/4.
//
//

#import "JYChatCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "JYUser.h"


@interface JYChatCell()

@property(weak,nonatomic) UIImageView *iconView;
@property(weak,nonatomic) UILabel *nameLabel;
@property(weak,nonatomic) UILabel *JYLabel;
@property(weak,nonatomic) UIButton *clickButton;

@end

@implementation JYChatCell


+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"chat";
    JYChatCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    return cell;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIImageView *iconView = [[UIImageView alloc]init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        
        UILabel *nameLabel = [[UILabel alloc]init];
        [self.contentView addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        UILabel *JYLabel = [[UILabel alloc]init];
        [self.contentView addSubview:JYLabel];
        self.JYLabel = JYLabel;
        
        UIButton *btn = [[UIButton alloc]init];
        [self.contentView addSubview:btn];
        self.clickButton = btn;
        self.backgroundColor = [UIColor colorWithRed:243 / 255.0 green:244 / 255.0 blue:245 / 255.0 alpha:1.0];
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat contentH = self.contentView.frame.size.height;
    CGFloat contentW = self.contentView.frame.size.width;
    CGFloat margin = 15;
    
    CGFloat iconX = margin;
    CGFloat iconY = margin;
    CGFloat iconH = contentH - 2 * iconY;
    CGFloat iconW = iconH;
    self.iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
    self.iconView.layer.cornerRadius = iconW * 0.5;
    self.iconView.clipsToBounds = YES;
    
    
    CGFloat titleX = CGRectGetMaxX(self.iconView.frame) + margin;
    CGFloat titleY = iconY;
    CGFloat titleW = contentW * 0.4;
    CGFloat titleH = 21;
    self.nameLabel.font = [UIFont systemFontOfSize: 14.0];
    self.nameLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);
    
    
    CGFloat priceX = titleX;
    CGFloat priceH = 21;
    CGFloat priceY = contentH - margin - priceH;
    CGFloat priceW = contentW - 3.5 * margin - iconW - 55;
    self.JYLabel.font = [UIFont systemFontOfSize: 10.0];
    self.JYLabel.frame = CGRectMake(priceX, priceY, priceW, priceH);
    
    
    CGFloat buyCountH = 22;
    CGFloat buyCountY = (contentH - buyCountH) * 0.5;
    CGFloat buyCountW = 55;
    CGFloat buyCountX = contentW - margin - buyCountW;
    self.clickButton.frame = CGRectMake(buyCountX, buyCountY, buyCountW, buyCountH);
}

-(void)setUser:(JYUser *)user{
    _user = user;
    [self.iconView sd_setImageWithURL:[self download:user.iconUrl]];
    self.nameLabel.text = user.nickName;
    self.JYLabel.text = user.signature;
    
    [self.clickButton setImage: [UIImage imageNamed:@"liaota"] forState:UIControlStateNormal];
    [self.clickButton addTarget:self action:@selector(chatting) forControlEvents:UIControlEventTouchUpInside];
}

-(void)chatting{
    NSDictionary *dict = @{@"uid":self.user.id};
    [[NSNotificationCenter defaultCenter]postNotificationName:@"chatting" object:nil userInfo:dict];
}

- (NSURL *)download:(NSString *)key{
    if ([key containsString:@"http://"] || [key containsString:@"https://"]) {
        return [NSURL URLWithString:key];
    } else {
        
        return [[NSURL alloc]initWithString:[[NSString alloc]initWithFormat:@"https://oizwa5jsl.qnssl.com/%@",key]];
    }
}
@end
